from django.core.exceptions import ValidationError
from django import forms


def password_validator(password):
    # 长度
    if len(password) < 16:
        raise ValidationError('密码长度不能小于16个字符')
    # 复杂度
    chars = set(password)
    nums = set('0123456789')
    upper = set(chr(i) for i in range(65, 91))
    lower = set(chr(i) for i in range(97, 123))
    puncts = set(chr(i) for i in range(33, 48))

    passed = True
    errmsg = []
    if not chars & nums:
        passed = False
        errmsg.append('密码必须包含数字')
    if not chars & upper:
        passed = False
        errmsg.append('密码必须包含大写字母')
    if not chars & lower:
        passed = False
        errmsg.append('密码必须包含小写字母')
    if not chars & puncts:
        passed = False
        errmsg.append('密码必须包含标点符号')
    if not passed:
        raise ValidationError(', '.join(errmsg))


class LoginForm(forms.Form):
    name = forms.CharField()
    password = forms.CharField()


class SignupForm(forms.Form):
    name = forms.CharField(max_length=8)
    password = forms.CharField(min_length=16, validators=[password_validator])
